/*
 * Node.h
 *
 *  Created on: 02-Sep-2014
 *      Author: ali
 */

#ifndef NODE_H_
#define NODE_H_
#include "Coordinate.h"
#include "Message.h"
#include "Channel.h"
#include <iostream>
using namespace std;
enum eNodeType
{
	eNodeNone, eNormalSensor, eCHSensor, eNodeSink
};

class Node
{
	int m_nID;
	double m_dResidualPower;
	Coordinate m_cMyPos;
	eNodeType m_eNodeType;
	int m_nCHSelectionWinner;
	bool m_bIsActive;
	Message m_cCurMsg;
	Message m_cDataMsg;

public:

	Node();
	Node(int id, double pow, const Coordinate& pos, eNodeType);
	double GetResidualPower();
	int GetID();
	void SetNodeType(eNodeType type)
	{
		m_eNodeType = type;
	}
	eNodeType GetNodeType()
	{
		return m_eNodeType;
	}
	void IncreaseCHSelectionWinner()
	{
		++m_nCHSelectionWinner;
	}

	bool Send(Node &);
	bool Send(Message&, double);
	bool SendCHtoCH(Message&, double);
	bool Recv(Message&);
	bool RecvCHSelectionMsg();
	bool SendCHSelectionMsg();

	bool IsBetterCH();
	Coordinate GetPos();
	bool GetNodeStatus();
	void UpdateNodeStatus();
	void SetNodeStatus(bool bState)
	{
		m_bIsActive = bState;
	}
	void AppendDataBuffer(int len)
	{
		m_cDataMsg.Append(len);
	}
	void InitDataBuffer()
	{
		m_cDataMsg.SetMessageLeninBits(0);
		m_cDataMsg.SetMessageType(eData);
	}
	bool SendData();
	friend ostream & operator<<(ostream &out, Node& n);
	~Node();
};

#endif /* NODE_H_ */
